package com.yonyou.gmmc.dealerApp.api.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.marketing.common.constants.BusinessConstant;
import org.marketing.common.constants.CodeConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.askingPrice.AskingPriceDto;
import org.marketing.common.dto.customerInfo.CustomerBindCarsDto;
import org.marketing.common.dto.customerInfo.LoveModel4SaleDto;
import org.marketing.common.dto.customerInfo.PotentialUser4AfterSaleDto;
import org.marketing.common.dto.customerInfo.PotentialUser4SaleDto;
import org.marketing.common.dto.customerInfo.PuserOwnerRelationDto;
import org.marketing.common.dto.customerInfo.PuserUserCarOwnerRelationDto;
import org.marketing.common.dto.customerInfo.TlNormalFollowRecodeDto;
import org.marketing.common.dto.freeTime.FreeTimeDto;
import org.marketing.common.dto.maintain.MaintainDto;
import org.marketing.common.dto.maintain.MaintainFrecordDto;
import org.marketing.common.dto.maintain.RepairBookParamDto;
import org.marketing.common.dto.maintain.RepairBookTimeDto;
import org.marketing.common.dto.maintain.SosHelpDto;
import org.marketing.common.dto.search.DealerAppSearchDto;
import org.marketing.common.dto.testdrivingAm.TestDrivingAmDto;
import org.marketing.common.exception.ServiceAppException;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.service.rest.RestServiceConstant;
import org.marketing.common.service.rest.RestServiceUtil;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.CommonUtils;
import org.marketing.common.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.ibm.icu.util.Calendar;
import com.yonyou.gmmc.dealerApp.api.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.dealerApp.api.dao.TtReservationMapper;
import com.yonyou.gmmc.dealerApp.api.dao.TtSoshelpMapper;
import com.yonyou.gmmc.dealerApp.api.entity.MaintainFrecordPO;
import com.yonyou.gmmc.dealerApp.api.entity.PuserOwnerRelationPO;
import com.yonyou.gmmc.dealerApp.api.entity.TlCarOwnerFollowRecodePO;
import com.yonyou.gmmc.dealerApp.api.entity.TlNormalFollowRecodePO;

import net.sf.json.JSONObject;

/**
 * @author kaihua 由于其他提供服务的微服务接口文档不完整，为了赶进度，目前所有实现放在本微服务自身中实现。
 *         后续有时间整理各个提供服务的微服务开放的功能接口之后，再通过调用其他微服务功能的方式调用功能
 *
 */
@Service
@Transactional
public class CallService {
	private Logger logger = Logger.getLogger(CallService.class);
	
	@Autowired
    private RepairBookingService repairBookingService;

	public Map<String, Object> getFreeTimeCludeWorkStation(RepairBookParamDto repairBookParamDto) {
		try {
            //校验TOKEN是否有效
//            if (RestServiceUtil.isTokenVolid(headers) == false) {
//                logger.info("receiveRepairBookingTimecomplex:" + RestServiceConstant.REST_CHECK_SECURITY_TIME_FAILED);
//                return generateFailResultMap(RestServiceConstant.REST_CHECK_SECURITY_TIME_FAILED, null);
//            }
            
            List<RepairBookTimeDto> resultTimeListDto = repairBookingService.getRepairBookTimeComplex(repairBookParamDto);
            
            //返回MAP设置
            return generateSuccessResultMap(resultTimeListDto);
            
        } catch (Exception e) {
            //返回结果，定义失败为404
            return generateFailResultMap(e.getMessage(), null);
        }
	}
	/**
     * 
    *
    * @author sangdeliang
    * @ description 生成返回的MAP对象(成功)
    * @date 2016年12月7日
    * @param statusCode
    * @param httpCode
    * @param msg
    * @param object
    * @return
     */
    private Map<String, Object> generateSuccessResultMap( Object object) {
        Map<String ,Object> resultMap = new HashMap<String, Object>();
        resultMap.put(RestServiceConstant.REST_BUSINESS_STATUS, RestServiceConstant.REST_BUSINESS_SUCCESS);
        resultMap.put(RestServiceConstant.REST_BUSINESS_HTTP_CODE, RestServiceConstant.HTTP_SUCCESS_CODE);
        resultMap.put(RestServiceConstant.REST_BUSINESS_MSG, RestServiceConstant.RESPONSE_HEADER_CONTENT_SUCCESS);
        if (object != null) {
            resultMap.put(RestServiceConstant.REST_BUSINESS_OBJECT, object);
        }
        return resultMap;
    }
	/**
     * 
    *
    * @author sangdeliang
    * @ description 生成返回的MAP对象(失败)
    * @date 2016年12月7日
    * @param errorMsg
    * @param object
    * @return
     */
    private Map<String, Object> generateFailResultMap(String errorMsg, Object object) {
        Map<String ,Object> resultMap = new HashMap<String, Object>();
        resultMap.put(RestServiceConstant.REST_BUSINESS_STATUS, RestServiceConstant.REST_BUSINESS_FAEL);
        resultMap.put(RestServiceConstant.REST_BUSINESS_HTTP_CODE, RestServiceConstant.HTTP_FAIL_CODE);
        resultMap.put(RestServiceConstant.REST_BUSINESS_MSG, errorMsg);
        if (object != null) {
            resultMap.put(RestServiceConstant.REST_BUSINESS_OBJECT, object);
        }
        return resultMap;
    }

}
